package com.yunfei.springbootgoods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunfei.springbootgoods.dto.Page;
import com.yunfei.springbootgoods.entity.Goods;
import com.yunfei.springbootgoods.mapper.GoodsMapper;
import com.yunfei.springbootgoods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void addGoods(Goods goods) {
        goodsMapper.addGoods(goods);
    }

    @Override
    public void deleteGoods(Integer goodsId) {
        goodsMapper.deleteGoods(goodsId);
    }

    @Override
    public void updateGoods(Goods goods) {
        goodsMapper.updateGoods(goods);
    }

    @Override
    public void updateStock(Integer goodsId) {
        goodsMapper.updateStock(goodsId);
    }

    @Override
    public Page getGoodsPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<Goods> goodsList = goodsMapper.getGoodsList();
        PageInfo<Goods> info = new PageInfo<>(goodsList);
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setCount((int) info.getTotal());
        page.setPageCount(info.getPages());
        page.setData(info.getList());
        return page;
    }

}
